<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>simpleresult.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_result_simpleresult.js.html">simpleresult.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A SimpleResult object that implements goog.result.Result.
<a name="line17"></a> * See below for a more detailed description.
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.result.SimpleResult&#39;);
<a name="line21"></a>goog.provide(&#39;goog.result.SimpleResult.StateError&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.Promise&#39;);
<a name="line24"></a>goog.require(&#39;goog.Thenable&#39;);
<a name="line25"></a>goog.require(&#39;goog.debug.Error&#39;);
<a name="line26"></a>goog.require(&#39;goog.result.Result&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * A SimpleResult object is a basic implementation of the
<a name="line32"></a> * goog.result.Result interface. This could be subclassed(e.g. XHRResult)
<a name="line33"></a> * or instantiated and returned by another class as a form of result. The caller
<a name="line34"></a> * receiving the result could then attach handlers to be called when the result
<a name="line35"></a> * is resolved(success or error).
<a name="line36"></a> *
<a name="line37"></a> * @constructor
<a name="line38"></a> * @implements {goog.result.Result}
<a name="line39"></a> * @deprecated Use {@link goog.Promise} instead - http://go/promisemigration
<a name="line40"></a> */
<a name="line41"></a>goog.result.SimpleResult = function() {
<a name="line42"></a>  /**
<a name="line43"></a>   * The current state of this Result.
<a name="line44"></a>   * @type {goog.result.Result.State}
<a name="line45"></a>   * @private
<a name="line46"></a>   */
<a name="line47"></a>  this.state_ = goog.result.Result.State.PENDING;
<a name="line48"></a>
<a name="line49"></a>  /**
<a name="line50"></a>   * The list of handlers to call when this Result is resolved.
<a name="line51"></a>   * @type {!Array.&lt;!goog.result.SimpleResult.HandlerEntry_&gt;}
<a name="line52"></a>   * @private
<a name="line53"></a>   */
<a name="line54"></a>  this.handlers_ = [];
<a name="line55"></a>
<a name="line56"></a>  // The value_ and error_ properties are initialized in the constructor to
<a name="line57"></a>  // ensure that all SimpleResult instances share the same hidden class in
<a name="line58"></a>  // modern JavaScript engines.
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * The &#39;value&#39; of this Result.
<a name="line62"></a>   * @type {*}
<a name="line63"></a>   * @private
<a name="line64"></a>   */
<a name="line65"></a>  this.value_ = undefined;
<a name="line66"></a>
<a name="line67"></a>  /**
<a name="line68"></a>   * The error slug for this Result.
<a name="line69"></a>   * @type {*}
<a name="line70"></a>   * @private
<a name="line71"></a>   */
<a name="line72"></a>  this.error_ = undefined;
<a name="line73"></a>};
<a name="line74"></a>goog.Thenable.addImplementation(goog.result.SimpleResult);
<a name="line75"></a>
<a name="line76"></a>
<a name="line77"></a>/**
<a name="line78"></a> * A waiting handler entry.
<a name="line79"></a> * @typedef {{
<a name="line80"></a> *   callback: !function(goog.result.SimpleResult),
<a name="line81"></a> *   scope: Object
<a name="line82"></a> * }}
<a name="line83"></a> * @private
<a name="line84"></a> */
<a name="line85"></a>goog.result.SimpleResult.HandlerEntry_;
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Error thrown if there is an attempt to set the value or error for this result
<a name="line91"></a> * more than once.
<a name="line92"></a> *
<a name="line93"></a> * @constructor
<a name="line94"></a> * @extends {goog.debug.Error}
<a name="line95"></a> * @final
<a name="line96"></a> * @deprecated Use {@link goog.Promise} instead - http://go/promisemigration
<a name="line97"></a> */
<a name="line98"></a>goog.result.SimpleResult.StateError = function() {
<a name="line99"></a>  goog.result.SimpleResult.StateError.base(this, &#39;constructor&#39;,
<a name="line100"></a>      &#39;Multiple attempts to set the state of this Result&#39;);
<a name="line101"></a>};
<a name="line102"></a>goog.inherits(goog.result.SimpleResult.StateError, goog.debug.Error);
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/** @override */
<a name="line106"></a>goog.result.SimpleResult.prototype.getState = function() {
<a name="line107"></a>  return this.state_;
<a name="line108"></a>};
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/** @override */
<a name="line112"></a>goog.result.SimpleResult.prototype.getValue = function() {
<a name="line113"></a>  return this.value_;
<a name="line114"></a>};
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/** @override */
<a name="line118"></a>goog.result.SimpleResult.prototype.getError = function() {
<a name="line119"></a>  return this.error_;
<a name="line120"></a>};
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Attaches handlers to be called when the value of this Result is available.
<a name="line125"></a> *
<a name="line126"></a> * @param {!function(this:T, !goog.result.SimpleResult)} handler The function
<a name="line127"></a> *     called when the value is available. The function is passed the Result
<a name="line128"></a> *     object as the only argument.
<a name="line129"></a> * @param {T=} opt_scope Optional scope for the handler.
<a name="line130"></a> * @template T
<a name="line131"></a> * @override
<a name="line132"></a> */
<a name="line133"></a>goog.result.SimpleResult.prototype.wait = function(handler, opt_scope) {
<a name="line134"></a>  if (this.isPending_()) {
<a name="line135"></a>    this.handlers_.push({
<a name="line136"></a>      callback: handler,
<a name="line137"></a>      scope: opt_scope || null
<a name="line138"></a>    });
<a name="line139"></a>  } else {
<a name="line140"></a>    handler.call(opt_scope, this);
<a name="line141"></a>  }
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * Sets the value of this Result, changing the state.
<a name="line147"></a> *
<a name="line148"></a> * @param {*} value The value to set for this Result.
<a name="line149"></a> */
<a name="line150"></a>goog.result.SimpleResult.prototype.setValue = function(value) {
<a name="line151"></a>  if (this.isPending_()) {
<a name="line152"></a>    this.value_ = value;
<a name="line153"></a>    this.state_ = goog.result.Result.State.SUCCESS;
<a name="line154"></a>    this.callHandlers_();
<a name="line155"></a>  } else if (!this.isCanceled()) {
<a name="line156"></a>    // setValue is a no-op if this Result has been canceled.
<a name="line157"></a>    throw new goog.result.SimpleResult.StateError();
<a name="line158"></a>  }
<a name="line159"></a>};
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/**
<a name="line163"></a> * Sets the Result to be an error Result.
<a name="line164"></a> *
<a name="line165"></a> * @param {*=} opt_error Optional error slug to set for this Result.
<a name="line166"></a> */
<a name="line167"></a>goog.result.SimpleResult.prototype.setError = function(opt_error) {
<a name="line168"></a>  if (this.isPending_()) {
<a name="line169"></a>    this.error_ = opt_error;
<a name="line170"></a>    this.state_ = goog.result.Result.State.ERROR;
<a name="line171"></a>    this.callHandlers_();
<a name="line172"></a>  } else if (!this.isCanceled()) {
<a name="line173"></a>    // setError is a no-op if this Result has been canceled.
<a name="line174"></a>    throw new goog.result.SimpleResult.StateError();
<a name="line175"></a>  }
<a name="line176"></a>};
<a name="line177"></a>
<a name="line178"></a>
<a name="line179"></a>/**
<a name="line180"></a> * Calls the handlers registered for this Result.
<a name="line181"></a> *
<a name="line182"></a> * @private
<a name="line183"></a> */
<a name="line184"></a>goog.result.SimpleResult.prototype.callHandlers_ = function() {
<a name="line185"></a>  var handlers = this.handlers_;
<a name="line186"></a>  this.handlers_ = [];
<a name="line187"></a>  for (var n = 0; n &lt; handlers.length; n++) {
<a name="line188"></a>    var handlerEntry = handlers[n];
<a name="line189"></a>    handlerEntry.callback.call(handlerEntry.scope, this);
<a name="line190"></a>  }
<a name="line191"></a>};
<a name="line192"></a>
<a name="line193"></a>
<a name="line194"></a>/**
<a name="line195"></a> * @return {boolean} Whether the Result is pending.
<a name="line196"></a> * @private
<a name="line197"></a> */
<a name="line198"></a>goog.result.SimpleResult.prototype.isPending_ = function() {
<a name="line199"></a>  return this.state_ == goog.result.Result.State.PENDING;
<a name="line200"></a>};
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/**
<a name="line204"></a> * Cancels the Result.
<a name="line205"></a> *
<a name="line206"></a> * @return {boolean} Whether the result was canceled. It will not be canceled if
<a name="line207"></a> *    the result was already canceled or has already resolved.
<a name="line208"></a> * @override
<a name="line209"></a> */
<a name="line210"></a>goog.result.SimpleResult.prototype.cancel = function() {
<a name="line211"></a>  // cancel is a no-op if the result has been resolved.
<a name="line212"></a>  if (this.isPending_()) {
<a name="line213"></a>    this.setError(new goog.result.Result.CancelError());
<a name="line214"></a>    return true;
<a name="line215"></a>  }
<a name="line216"></a>  return false;
<a name="line217"></a>};
<a name="line218"></a>
<a name="line219"></a>
<a name="line220"></a>/** @override */
<a name="line221"></a>goog.result.SimpleResult.prototype.isCanceled = function() {
<a name="line222"></a>  return this.state_ == goog.result.Result.State.ERROR &amp;&amp;
<a name="line223"></a>         this.error_ instanceof goog.result.Result.CancelError;
<a name="line224"></a>};
<a name="line225"></a>
<a name="line226"></a>
<a name="line227"></a>/** @override */
<a name="line228"></a>goog.result.SimpleResult.prototype.then = function(
<a name="line229"></a>    opt_onFulfilled, opt_onRejected, opt_context) {
<a name="line230"></a>  var resolve, reject;
<a name="line231"></a>  // Copy the resolvers to outer scope, so that they are available
<a name="line232"></a>  // when the callback to wait() fires (which may be synchronous).
<a name="line233"></a>  var promise = new goog.Promise(function(res, rej) {
<a name="line234"></a>    resolve = res;
<a name="line235"></a>    reject = rej;
<a name="line236"></a>  });
<a name="line237"></a>  this.wait(function(result) {
<a name="line238"></a>    if (result.isCanceled()) {
<a name="line239"></a>      promise.cancel();
<a name="line240"></a>    } else if (result.getState() == goog.result.Result.State.SUCCESS) {
<a name="line241"></a>      resolve(result.getValue());
<a name="line242"></a>    } else if (result.getState() == goog.result.Result.State.ERROR) {
<a name="line243"></a>      reject(result.getError());
<a name="line244"></a>    }
<a name="line245"></a>  });
<a name="line246"></a>  return promise.then(opt_onFulfilled, opt_onRejected, opt_context);
<a name="line247"></a>};
<a name="line248"></a>
<a name="line249"></a>
<a name="line250"></a>/**
<a name="line251"></a> * Creates a SimpleResult that fires when the given promise resolves.
<a name="line252"></a> * Use only during migration to Promises.
<a name="line253"></a> * @param {!goog.Promise.&lt;?&gt;} promise
<a name="line254"></a> * @return {!goog.result.Result}
<a name="line255"></a> */
<a name="line256"></a>goog.result.SimpleResult.fromPromise = function(promise) {
<a name="line257"></a>  var result = new goog.result.SimpleResult();
<a name="line258"></a>  promise.then(result.setValue, result.setError, result);
<a name="line259"></a>  return result;
<a name="line260"></a>};
</pre>


</body>
</html>
